package com.zklh.leetcode.top100;

import com.zklh.leetcode.tree.model.TreeNode;

import java.util.*;

/**
 * 二叉树的右视图
 *
 * @version 1.0
 * @Author tongnan
 * @Date 2023/10/8
 */
public class Solution199 {
    public List<Integer> rightSideView(TreeNode root) {
        Map<Integer, Integer> depthToValue = new HashMap<>();
        List<Integer> rightSideView = new ArrayList<>();
        preorderTraversal(root, 0, depthToValue);
        for (int i = 0; i < depthToValue.size(); i++) {
            rightSideView.add(depthToValue.get(i));
        }
        return rightSideView;
    }

    private void preorderTraversal(TreeNode node, int depth, Map<Integer, Integer> depthToValue) {
        if (node == null) {
            return;
        }
        depthToValue.put(depth, node.val);
        preorderTraversal(node.left, depth+1, depthToValue);
        preorderTraversal(node.right, depth+1, depthToValue);
    }
}